﻿/// -----------------------------------------------------------------
/// MapManipulator.cs: provides methods for extracting, 
/// updating and deleting Maps in the Database.
/// License: see http://mve.codeplex.com/license; All Rights follows the MS-PL
/// Current owner: shiniwa
/// The project decription: please refer to http://codeplex.com/mve/
/// -----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace MVEContentSvc.MapOperations
{
    /*
     * 日本SGI株式会社
     * ヨハンソン・ヘンリク
     * johansson@sgi.co.jp
     * 
     * Change Log:
     * ---------------------------------------------------------------------------
     * 2009.01.07	Johansson Henrik		Class Created.
     * 
     */

    /// <summary>
    /// This static class provides methods for extracting, 
    /// updating and deleting Maps in the Database.
    /// Use the static methods in this class to perform  
    /// Map related operations on the Database.
    /// </summary>
    /// <remarks>INSERT:Creds</remarks>
    internal static class MapManipulator
    {
        /// <summary>
        /// Get the users map from the DB.
        /// Original Name = LoadMap
        /// </summary>
        /// <remarks>Done</remarks>
        /// <param name="userDbId">Users DB id</param>
        /// <param name="connection">Sql Connection</param>
        /// <returns>A string (XML) representation of the users map.</returns>
        internal static string GetUsersMap(int userDbId, SqlConnection connection)
        {
            SqlCommand command = new SqlCommand();
            SqlDataReader reader = null;
            command.CommandTimeout = 60;

            string result = string.Empty;
            try
            {
                command.Connection = connection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "GetMap";

                command.Parameters.Add("userId", SqlDbType.Int).Value = userDbId;

                reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    reader.Read();
                    result = reader.GetString(0);
                }

            }
            catch (NullReferenceException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = string.Empty;
            }
            catch (SqlException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = string.Empty;
            }
            catch (InvalidOperationException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = string.Empty;
            }
            catch (IndexOutOfRangeException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = string.Empty;
            }
            finally
            {
                command.Dispose();
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }

            return result;
        }//end:GetUsersMap

        /// <summary>
        /// Save / Update a users map in the Database.
        /// Original Name = SaveMap
        /// </summary>
        /// <remarks>Done</remarks>
        /// <param name="userDbId">Users DB id</param>
        /// <param name="map">A string (XML) representation of the map to save</param>
        /// <param name="connection">Sql Connection</param>
        /// <returns>True if the map was saved</returns>
        internal static bool SaveUsersMap(int userDbId, string map, SqlConnection connection)
        {
            SqlCommand command = new SqlCommand();
            command.CommandTimeout = 60;

            bool result = false;
            try
            {
                command.Connection = connection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandText = "SaveMap";

                command.Parameters.Add("userId", SqlDbType.Int).Value = userDbId;
                command.Parameters.Add("map", SqlDbType.Text).Value = map;
                command.Parameters.Add("insertDate", SqlDbType.DateTime).Value = DateTime.Now;

                SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
                returnValue.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(returnValue);

                command.ExecuteNonQuery();
                int resultCode = (int)returnValue.Value;

                if (resultCode == 0)
                {
                    result = true;
                }
            }
            catch (InvalidOperationException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = false;
            }
            catch (InvalidCastException ex)
            {
                ErrorHandler.LogMessage(ex.Message, false);
                result = false;
            }
            finally
            {
                command.Dispose();
            }

            return result;
        }//end:SaveUsersMap
    }
}
